package com.jiantong.work.impl.service.yangzhibaoxian;

import com.google.common.base.Throwables;
import com.jiantong.work.impl.dao.yangzhibaoxian.InsuranceApplicationDao;
import com.jiantong.work.model.yangzhibaoxian.InsuranceApplication;
import com.jiantong.work.service.yangzhibaoxian.InsuranceApplicationService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.PageInfo;
import io.terminus.common.model.Paging;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by ThinkPad on 2017/8/30.
 */
@Slf4j
@Service
@RpcProvider
public class InsuranceApplicationServiceImpl implements InsuranceApplicationService {

    private InsuranceApplicationDao insuranceApplicationDao;

    @Autowired
    public InsuranceApplicationServiceImpl(InsuranceApplicationDao insuranceApplicationDao) {
        this.insuranceApplicationDao = insuranceApplicationDao;
    }

    @Override
    public Response<InsuranceApplication> findBynumber(String number) {

        try {
            InsuranceApplication insuranceApplication = insuranceApplicationDao.findBynumber(number);
            if (insuranceApplication == null) {
                log.error("immunizationRecord(number={}) is not found", number);
                return Response.fail("insuranceApplication.not.found");
            }

            return Response.ok(insuranceApplication);
        } catch (Exception e) {
            log.error("failed to find insuranceApplication(number={}), cause:{}", number, Throwables.getStackTraceAsString(e));
            return Response.fail("insuranceApplication.find.fail");
        }

    }

    @Override
    public Response<InsuranceApplication> findById(Long id) {
        try {
            InsuranceApplication insuranceApplication = (InsuranceApplication) insuranceApplicationDao.findById(id);
            if (insuranceApplication == null) {
                log.error("immunizationRecord(id={}) is not found", id);
                return Response.fail("insuranceApplication.not.found");
            }

            return Response.ok(insuranceApplication);
        } catch (Exception e) {
            log.error("failed to find insuranceApplication(id={}), cause:{}", id, Throwables.getStackTraceAsString(e));
            return Response.fail("insuranceApplication.find.fail");
        }

    }

    @Override
    public Response<List<InsuranceApplication>> findByIds(List ids) {
        try {
            List<InsuranceApplication> insuranceApplications =  insuranceApplicationDao.findByIds(ids);
            if (insuranceApplications == null) {
                log.error("immunizationRecord(ids={}) is not found", ids);
                return Response.fail("insuranceApplication.not.found");
            }

            return Response.ok(insuranceApplications);
        } catch (Exception e) {
            log.error("failed to find insuranceApplication(ids={}), cause:{}", ids, Throwables.getStackTraceAsString(e));
            return Response.fail("insuranceApplication.find.fail");
        }

    }

    @Override
    public Response<List<InsuranceApplication>> list(Map map) {

        try {
            List<InsuranceApplication> insuranceApplications =  insuranceApplicationDao.list(map);
            if (insuranceApplications == null) {
                log.error("immunizationRecord(map={}) is not found", map);
                return Response.fail("insuranceApplication.not.found");
            }

            return Response.ok(insuranceApplications);
        } catch (Exception e) {
            log.error("failed to find insuranceApplication(map={}), cause:{}", map, Throwables.getStackTraceAsString(e));
            return Response.fail("insuranceApplication.find.fail");
        }

    }

    @Override
    public Response<Paging<InsuranceApplication>> ping(Integer offset, Integer limit,Map map) {
        try {
            PageInfo pageInfo=new PageInfo(offset,limit);
            Paging<InsuranceApplication> insuranceApplications =  insuranceApplicationDao.paging(pageInfo.getOffset(), pageInfo.getLimit(),map);
            if (insuranceApplications == null) {
                log.error("immunizationRecord(map={}) is not found", map);
                return Response.fail("insuranceApplication.not.found");
            }

            return Response.ok(insuranceApplications);
        } catch (Exception e) {
            log.error("failed to find insuranceApplication(map={}), cause:{}", map, Throwables.getStackTraceAsString(e));
            return Response.fail("insuranceApplication.find.fail");
        }
    }

    @Override
    public Boolean update(InsuranceApplication insuranceApplication) {
        try {
            Boolean b=insuranceApplicationDao.update(insuranceApplication);
            return b;
        }catch (Exception e){
           return false;
        }

    }

    @Override
    public Long create(InsuranceApplication insuranceApplication) {
        try {
           insuranceApplicationDao.create(insuranceApplication);
           return insuranceApplication.getId();
        }catch (Exception e){
            e.printStackTrace();
            return -500L;
        }
    }

    @Override
    public Integer creates(List<InsuranceApplication> insuranceApplicationList) {
        try {
            return insuranceApplicationDao.creates(insuranceApplicationList);
        }catch (Exception e){
            e.printStackTrace();
            return -500;
        }
    }
}
